package service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import model.Tpayment;
import model.Treceipt;
import model.Tvip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pageModel.DataGrid;
import pageModel.Payment;
import dao.PaymentDaoI;
import dao.ReceiptDaoI;
import dao.VipDaoI;
import service.PaymentServiceI;
@Service("paymentService")
public class PaymentServiceImpl implements PaymentServiceI {
private PaymentDaoI paymentDao;
private ReceiptDaoI receiptDao;
private VipDaoI vipDao;
	public PaymentDaoI getPaymentDao() {
	return paymentDao;
}
@Autowired
public void setPaymentDao(PaymentDaoI paymentDao) {
	this.paymentDao = paymentDao;
}

	public ReceiptDaoI getReceiptDao() {
	return receiptDao;
}
	@Autowired
public void setReceiptDao(ReceiptDaoI receiptDao) {
	this.receiptDao = receiptDao;
}
	
	public VipDaoI getVipDao() {
		return vipDao;
	}
	@Autowired
	public void setVipDao(VipDaoI vipDao) {
		this.vipDao = vipDao;
	}
	@Override
	public void save(String tid,String pid,String vip,String method,String totalprice,String collection,String changes) {
		Tpayment t=new Tpayment();
		Treceipt tr=new Treceipt();
		if(tid!=null&&!tid.trim().equals("")){
			t.setId(UUID.randomUUID().toString());
	        t.setPid(pid);
	        t.setMethod(method);
	        t.setTotalprice(Double.parseDouble(totalprice));
	        t.setCollection(Double.parseDouble(collection));
	        t.setChanges(Double.parseDouble(changes));
			String hql="from Treceipt t where t.tid=:tid";
			 Map<String,Object> params=new HashMap<String,Object>();
			 params.put("tid",tid);
			 tr=receiptDao.get(hql,params);
			 t.setTreceipt(tr);
		}
		Timestamp now = new Timestamp(System.currentTimeMillis()); 
		t.setCreatedatetime(now);
		paymentDao.save(t);
		tr.setState("支付完成");
		receiptDao.save(tr);
		if(vip!=null&&!vip.trim().equals("")){
			String hql="from Tvip t where t.name=:name";
			Map<String,Object> params=new HashMap<String,Object>();
			params.put("name", vip);
			Tvip v=vipDao.get(hql,params);			  
			v.setIntegration(v.getIntegration()+Double.parseDouble(totalprice));
			v.setUpdatedatetime(new Timestamp(System.currentTimeMillis()));
			vipDao.save(v);
			   }
	}
	@Override
	public DataGrid datagrid(String cashier,Payment payment) {
		DataGrid dg=new DataGrid();
		String hql="from Tpayment t where t.treceipt.tuser.logname=:logname";
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("logname",cashier);
		String totalHql="select count(*) "+hql;
		if(payment.getSort()!=null){
		hql+=" order by "+payment.getSort()+" "+payment.getOrder();
		}
		List<Tpayment> tp=paymentDao.find(hql,params,payment.getPage(),payment.getRows());
		List<Payment> l=new ArrayList<Payment>();
		if(tp!=null&&tp.size()>0){
			for(Tpayment t :tp){
				Payment p=new Payment();
				p.setPid(t.getPid());
				p.setCreatedatetime(t.getCreatedatetime());
				p.setTotalprice(t.getTotalprice().toString());
				p.setCollection(t.getCollection().toString());
				p.setChanges(t.getChanges().toString());
				if(t.getTreceipt()!=null){
					p.setTid(t.getTreceipt().getTid());
				}
				l.add(p);
			}
		}
		dg.setTotal(paymentDao.count(totalHql,params));
		dg.setRows(l);
		return dg;
	}
}
